//
//  DownPaymentView.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/8/26.
//
//

#import "DownPaymentView.h"

@interface DownPaymentView ()
@property (nonatomic, strong) UIView *infoView;
@property (nonatomic, strong) UIView *paymentView;
@property (nonatomic, strong) UISegmentedControl *sc;

//期数按钮
@property (nonatomic, strong) UIButton *btn0;
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;
@end

@implementation DownPaymentView
#pragma mark - 生命周期 life cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self paymentView];
    }
    return self;
}

#pragma mark - 方法 methods
- (void)changePayNum:(UISlider *)sender {
    NSInteger value = (int)(sender.value * 10);
    [self.sc setSelectedSegmentIndex:value];
}

- (void)changeSelectedState:(UIButton *)sender {
    [self.btn0 setSelected:NO];
    [self.btn1 setSelected:NO];
    [self.btn2 setSelected:NO];
    sender.selected = !sender.isSelected;
}

#pragma mark - 懒加载 lazy loading
- (UIView *)infoView {
    if(_infoView == nil) {
        _infoView = [[UIView alloc] init];
        _infoView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_infoView];
        [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(0);
            make.top.equalTo(15);
            make.height.equalTo(70 * AUTO_WIDTH);
            make.width.equalTo(kScreenW - 90 * AUTO_WIDTH);
        }];
        _infoView.layer.borderWidth = 1;
        _infoView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        
        //首付LB
        UILabel *firstPaymentLB = [UILabel new];
        [_infoView addSubview:firstPaymentLB];
        [firstPaymentLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(0);
            make.width.equalTo((kScreenW - 90 * AUTO_WIDTH) / 3);
        }];
        firstPaymentLB.text = @"首付\n224100元";
        myLBSettings(firstPaymentLB);
        
        //分割线
        UIView *lineView = [UIView new];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [_infoView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(0);
            make.left.equalTo(firstPaymentLB.mas_right).equalTo(0);
            make.width.equalTo(1);
        }];
        
        //期数LB
        UILabel *payNumberLB = [UILabel new];
        [_infoView addSubview:payNumberLB];
        [payNumberLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(0);
            make.left.equalTo(lineView.mas_right).equalTo(0);
            make.width.equalTo((kScreenW - 90 * AUTO_WIDTH) / 3);
        }];
        payNumberLB.text = @"期数\n36期";
        myLBSettings(payNumberLB);
        
        //分割线
        UIView *lineView1 = [UIView new];
        lineView1.backgroundColor = [UIColor lightGrayColor];
        [_infoView addSubview:lineView1];
        [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(payNumberLB.mas_right).equalTo(0);
            make.top.bottom.equalTo(0);
            make.width.equalTo(1);
        }];
        
        //月供LB
        UILabel *paymentLB = [UILabel new];
        [_infoView addSubview:paymentLB];
        [paymentLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lineView1.mas_right).equalTo(0);
            make.top.bottom.equalTo(0);
            make.right.equalTo(0);
        }];
        paymentLB.text = @"月供\n18080元起";
        myLBSettings(paymentLB);
        
    }
    return _infoView;
}

- (UIView *)paymentView {
    if(_paymentView == nil) {
        _paymentView = [[UIView alloc] init];
        _paymentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_paymentView];
        [_paymentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(0);
            make.top.equalTo(self.infoView.mas_bottom).equalTo(15);
            make.height.equalTo(135 * AUTO_WIDTH);
            make.width.equalTo(kScreenW - 60 * AUTO_WIDTH);
        }];
        _paymentView.layer.borderWidth = 1;
        _paymentView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _paymentView.layer.cornerRadius = 8;
        
        //首付滑动选择视图
        UIView *chooseFisrtPayNumView = [UIView new];
        [_paymentView addSubview:chooseFisrtPayNumView];
        [chooseFisrtPayNumView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(0);
            make.height.equalTo(135 * AUTO_WIDTH / 2 - 1);
        }];
        
        UILabel *payTipLB = [UILabel new];
        [chooseFisrtPayNumView addSubview:payTipLB];
        [payTipLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(22 * AUTO_WIDTH);
            make.centerY.equalTo(0);
        }];
        payTipLB.text = @"首付";
        payTipLB.font = defaultFont;
        
        UISegmentedControl *numSC = [[UISegmentedControl alloc] initWithItems:@[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"]];
        self.sc = numSC;
        [chooseFisrtPayNumView addSubview:numSC];
        CGFloat scHeight = 20 * AUTO_WIDTH;
        [numSC mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(payTipLB.mas_right).equalTo(8 * AUTO_WIDTH);
            make.top.equalTo(8 * AUTO_WIDTH);
            make.right.equalTo(-20 * AUTO_WIDTH);
            make.height.equalTo(scHeight);
        }];
        [numSC setSelectedSegmentIndex:0];
        [numSC setTintColor:[UIColor whiteColor]];
        [numSC setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
        [numSC setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
        
        UISlider *slider = [[UISlider alloc] init];
        [chooseFisrtPayNumView addSubview:slider];
        [slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(payTipLB.mas_right).equalTo(12 * AUTO_WIDTH);
            make.top.equalTo(numSC.mas_bottom).equalTo(8 * AUTO_WIDTH);
            make.right.equalTo(-30 * AUTO_WIDTH);
            make.height.equalTo(scHeight);
        }];
        [slider addTarget:self action:@selector(changePayNum:) forControlEvents:UIControlEventValueChanged];
        
        //分割线
        UIView *lineView = [UIView new];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [_paymentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(20 * AUTO_WIDTH);
            make.right.equalTo(-20 * AUTO_WIDTH);
            make.top.equalTo(chooseFisrtPayNumView.mas_bottom).equalTo(0);
            make.height.equalTo(1);
        }];
        
        //期数按钮选择视图
        UIView *payNumView = [UIView new];
        [_paymentView addSubview:payNumView];
        [payNumView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.top.equalTo(lineView.mas_bottom).equalTo(0);
        }];
        
        UILabel *payNumTipLB = [UILabel new];
        [payNumView addSubview:payNumTipLB];
        [payNumTipLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(22 * AUTO_WIDTH);
            make.centerY.equalTo(0);
        }];
        payNumTipLB.text = @"期数";
        payNumTipLB.font = defaultFont;
        
        CGFloat btnWidth = 55 * AUTO_WIDTH;
        CGFloat btnLeftWidth = 15 * AUTO_WIDTH;
        CGFloat btnHeight = 30 * AUTO_WIDTH;
        UIButton *numBtn0 = [UIButton buttonWithType:UIButtonTypeSystem];
        self.btn0 = numBtn0;
        [payNumView addSubview:numBtn0];
        [numBtn0 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(payNumTipLB.mas_right).equalTo(btnLeftWidth);
            make.centerY.equalTo(0);
            make.width.equalTo(btnWidth);
            make.height.equalTo(btnHeight);
        }];
        [numBtn0 setTitle:@"12" forState:UIControlStateNormal];
        myBtnSettings(1, [[UIColor lightGrayColor] CGColor], [UIColor lightGrayColor], 6, numBtn0);
        [numBtn0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if ([self respondsToSelector:@selector(changeSelectedState:)]) {
            [numBtn0 addTarget:self action:@selector(changeSelectedState:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        UIButton *numBtn1 = [UIButton buttonWithType:UIButtonTypeSystem];
        self.btn1 = numBtn1;
        [payNumView addSubview:numBtn1];
        [numBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(numBtn0.mas_right).equalTo(btnLeftWidth);
            make.centerY.equalTo(0);
            make.width.equalTo(btnWidth);
            make.height.equalTo(btnHeight);
        }];
        [numBtn1 setTitle:@"24" forState:UIControlStateNormal];
        myBtnSettings(1, [[UIColor lightGrayColor] CGColor], [UIColor lightGrayColor], 6, numBtn1);
        [numBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if ([self respondsToSelector:@selector(changeSelectedState:)]) {
            [numBtn1 addTarget:self action:@selector(changeSelectedState:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        UIButton *numBtn2 = [UIButton buttonWithType:UIButtonTypeSystem];
        self.btn2 = numBtn2;
        [payNumView addSubview:numBtn2];
        [numBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(numBtn1.mas_right).equalTo(btnLeftWidth);
            make.centerY.equalTo(0);
            make.width.equalTo(btnWidth);
            make.height.equalTo(btnHeight);
        }];
        [numBtn2 setTitle:@"36" forState:UIControlStateNormal];
        myBtnSettings(1, [[UIColor lightGrayColor] CGColor], [UIColor lightGrayColor], 6, numBtn2);
        [numBtn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if ([self respondsToSelector:@selector(changeSelectedState:)]) {
            [numBtn2 addTarget:self action:@selector(changeSelectedState:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        UILabel *tip = [UILabel new];
        [payNumView addSubview:tip];
        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(numBtn2.mas_right).equalTo(10 * AUTO_WIDTH);
            make.centerY.equalTo(0);
        }];
        tip.text = @"期";
        tip.font = [UIFont systemFontOfSize:15 * AUTO_WIDTH];
        
    }
    return _paymentView;
}

@end
